package com.netcross.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.android.tu.loadingdialog.LoadingDailog;
import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
import com.google.gson.Gson;
import com.netcross.myapplication.bean.TrainMain;

import java.util.List;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class UpdateActivity extends AppCompatActivity {
    private Handler handler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                Intent intent = new Intent(UpdateActivity.this, BaseActivity.class);
                startActivity(intent);
                finish();
            }

            if (msg.what == 2) {

                List<PyObject> jsonList = (List<PyObject>) msg.obj;
                Log.d("python113",jsonList.toString());
                EditText editView1 = findViewById(R.id.demand_order_numberEditView);
                EditText editText2 = findViewById(R.id.waybill_idEditView);
                EditText editText3 = findViewById(R.id.type_of_transportEditView);
                EditText editText4 = findViewById(R.id.mode_of_transportEditView);
                EditText editText5 = findViewById(R.id.dangerous_goods_labeEditView);
                Gson gson = new Gson();
                for (int i = 0; i < jsonList.size(); i++) {

                    //不要怀疑,当前for循环次数为1,为以后勾选多个同时修改做准备
                    String strTypeJson = String.valueOf(jsonList.get(i));
                    TrainMain trainMain = gson.fromJson(strTypeJson, TrainMain.class);
                    // JSONObject jsonObject = JSONUtil.parseObj(strTypeJson);

                    editView1.setText(trainMain.getDemand_order_number());
                    editView1.setInputType(InputType.TYPE_NULL);
                    editText2.setText(trainMain.getWaybill_id());
                    editText3.setText(trainMain.getType_of_transport());
                    editText4.setText(trainMain.getMode_of_transport());
                    editText5.setText(trainMain.getDangerous_goods_labe());
                }
            }
        }
    };

    private long mBackPressed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);

//         LoadingDailog.Builder loadBuilder=new LoadingDailog.Builder(UpdateActivity.this)
//                .setMessage("加载中...")
//                 .setCancelable(false)
//                 .setCancelOutside(true)
//                .setCancelable(true)
//                .setCancelOutside(true);
//        LoadingDailog dialog=loadBuilder.create();
//        dialog.show();



        new Thread(new Runnable() {
            @Override
            public void run() {
                initPython();
                Intent intent = getIntent();
                List<PyObject> jsonList = callPythonCode(intent.getStringExtra("demand_order_number"));
                Message message = new Message();
                message.what = 2;
                message.obj = jsonList;
                handler.sendMessage(message);
            }
        }).start();



    }


    public void saveButtonOnCLick(View view) {
        EditText editView1 = findViewById(R.id.demand_order_numberEditView);
        EditText editText2 = findViewById(R.id.waybill_idEditView);
        EditText editText3 = findViewById(R.id.type_of_transportEditView);
        EditText editText4 = findViewById(R.id.mode_of_transportEditView);
        EditText editText5 = findViewById(R.id.dangerous_goods_labeEditView);

        new Thread(new Runnable() {
            @Override
            public void run() {
                Python py = Python.getInstance();
                PyObject obj = py.getModule("wbc").get("insertData").call(editView1.getText().toString(), editText2.getText().toString(), editText3.getText().toString(), editText4.getText().toString(), editText5.getText().toString());
                Message message = new Message();
                message.what = 1;
                handler.sendMessage(message);
            }
        }).start();


    }

    // 初始化Python环境
    void initPython() {
        if (!Python.isStarted()) {
            Python.start(new AndroidPlatform(this));
        }
    }


    List<PyObject> callPythonCode(String demand_order_number) {
        Python py = Python.getInstance();
        PyObject obj = py.getModule("wbc").get("getData").call(demand_order_number);
        List<PyObject> jsonList = obj.asList();
        Log.d("python123", jsonList.toString());
        return jsonList;

    }

    //点两次退出键后退出程序
    @Override
    public void onBackPressed() {
        Intent intent = new Intent(this, BaseActivity.class);
        this.startActivity(intent);
        finish();
    }

}